<template>
  <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="0">
    <el-form-item prop="phone">
      <el-input v-model.number="ruleForm.phone" placeholder="请输入用户名" />
    </el-form-item>

    <div class="flex">
      <el-form-item prop="phone" class="mr8 w-100">
        <el-input v-model="ruleForm.verificationCode" placeholder="请输入验证码" class="w-100" />
      </el-form-item>
      <el-button @click="handleGetCode">{{ timeCount == 60 ? "获取验证码" : timeCount }}</el-button>
    </div>

    <el-form-item prop="password">
      <el-input v-model="ruleForm.password" type="password" placeholder="8到16位同时包含大小写字母数字以及特殊符号的字符" autocomplete="off"
        show-password />
    </el-form-item>
    <el-form-item prop="confirmPassword">
      <el-input v-model="ruleForm.confirmPassword" type="password" placeholder="确认新密码" autocomplete="off" show-password />
    </el-form-item>

    <el-button type="primary" @click="submitForm(ruleFormRef)" class="w-100"> 修改密码 </el-button>
  </el-form>
</template>

<script lang="ts" setup>
import apiMod from '@/apiMod'
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'

const ruleFormRef = ref<FormInstance>()

const emit = defineEmits(['refresh'])
const checkPhone = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error('请输入手机号'))
  } else {
    callback()
  }
}

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    const reg = /^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\W]).{8,16}$/
    if (!reg.test(value)) {
      callback(new Error('密码必须为8到16位同时包含大小写字母数字以及特殊符号的字符'))
    } else if (ruleForm.confirmPassword !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('checkPass', () => null)
    }
    callback()
  }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== ruleForm.password) {
    callback(new Error('两次密码不一致!'))
  } else {
    callback()
  }
}

const ruleForm: any = reactive({
  password: '',
  confirmPassword: '',
  phone: '',
  verificationCode: ''
})

const rules = reactive<any>({
  password: [{ validator: validatePass, trigger: 'blur' }],
  confirmPassword: [{ validator: validatePass2, trigger: 'blur' }],
  phone: [{ validator: checkPhone, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      apiMod.sys_user_changePwd({ ...ruleForm }).then(res => {
        ElMessage.success(res.data)
        emit('refresh')
      })
    } else {
      return false
    }
  })
}

let timer = null
const timeCount = ref(60)
const handleGetCode = () => {
  let reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
  if (!reg.test(ruleForm.phone)) {
    ElMessage.warning('请输入正确的手机号!')
    return false
  }
  if (timeCount.value < 60) {
    return
  }
  apiMod.sendVerificationCode({
    phoneNumbers: ruleForm.phone,
    templateType: 1,
  }).then(() => {
    ElMessage.success('验证码已发送!')
    timer = setInterval(() => {
      if (timeCount.value == 0) {
        clearInterval(timer)
        timeCount.value = 60
      } else {
        timeCount.value--
      }
    }, 1000)
  })
}
</script>
